package top.yangbuyi.service.impl;

import javax.annotation.Resource;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import top.yangbuyi.constants.Constants;
import top.yangbuyi.domain.Patient;
import top.yangbuyi.domain.PatientFile;
import top.yangbuyi.dto.PatientDto;
import top.yangbuyi.mapper.PatientFileMapper;
import top.yangbuyi.mapper.PatientMapper;
import top.yangbuyi.service.PatientService;
import top.yangbuyi.utils.AppMd5Utils;
import top.yangbuyi.vo.DataGridView;

/**
 * ClassName: Loginfo
 * Description: 杨不易网站 :www.yangbuyi.top
 * date: 2020/9/14
 *
 * @author TeouBle
 * @author yangbuyi
 * @since JDK 1.8
 **/

@Service
public class PatientServiceImpl implements  PatientService{
	  /**
	   * @功能描述:患者信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/15 11:08
	   */
	  @Autowired
	  private PatientMapper patientMapper;

	  /**
	   * @功能描述:患者档案
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/15 11:08
	   */
	  @Autowired
	  private PatientFileMapper patientFileMapper;

	  /**
	   * @param patientDto
	   * @功能描述:分页查询所有患者信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/15 10:33
	   */
	  @Override
	  public DataGridView listPatientForPage(PatientDto patientDto) {
			Page<Patient> page = new Page<>(patientDto.getPageNum(), patientDto.getPageSize());
			QueryWrapper<Patient> qw = new QueryWrapper<>();
			qw.like(StringUtils.isNotBlank(patientDto.getName()), Patient.COL_NAME, patientDto.getName());
			qw.like(StringUtils.isNotBlank(patientDto.getIdCard()), Patient.COL_ID_CARD, patientDto.getIdCard());
			qw.like(StringUtils.isNotBlank(patientDto.getPhone()), Patient.COL_PHONE, patientDto.getPhone());
			this.patientMapper.selectPage(page, qw);
			System.out.println( page.getRecords());
			return new DataGridView(page.getTotal(), page.getRecords());
	  }

	  /**
	   * @param patientId
	   * @功能描述:根据患者ID查询患者信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/15 10:30
	   */
	  @Override
	  public Patient getPatientById(String patientId) {
			return this.patientMapper.selectById(patientId);
	  }

	  /**
	   * @param patientId
	   * @功能描述:根据患者ID查询患者的档案信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/15 10:30
	   */
	  @Override
	  public PatientFile getPatientFileById(String patientId) {
			return this.patientFileMapper.selectById(patientId);
	  }

	/**
	 * 根据患者身份证查询患者信息
	 * @param idCard
	 * @return
	 */
	@Override
    public Patient getPatientByIdCard(String idCard) {
		Wrapper<Patient> wrapper=new QueryWrapper<>();
		((QueryWrapper<Patient>) wrapper).eq(Patient.COL_ID_CARD,idCard);
		return this.patientMapper.selectOne(wrapper);
    }

    /**
     * 挂号时，患者信息不存在是。添加患者信息
     * @param patientDto
     * @return
     */
    @Override
    public Patient addPatient(PatientDto patientDto) {
        Patient patient=new Patient();
        BeanUtil.copyProperties(patientDto,patient);
        patient.setCreateTime(DateUtil.date());
        patient.setIsFinal(Constants.IS_FINAL_FALSE);
        String defaultPwd=patient.getPhone().substring(5);
        patient.setPassword(AppMd5Utils.md5(defaultPwd,patient.getPhone(),2));
        this.patientMapper.insert(patient);
        return patient;
    }
}
